package com.example.sportgoods.controller;

import com.example.sportgoods.domain.Shop;
import com.example.sportgoods.domain.User;
import com.example.sportgoods.Service.UserService;
import com.example.sportgoods.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理（0528王雪伊）")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "取得所有用户列表", notes = "取得所有用户列表")
    @GetMapping("/list")
    private AjaxResult list(){
        List<User> allUsers =userService.getUsers();
        return AjaxResult.success(allUsers);
    }

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @PostMapping("/insertuser")
    public AjaxResult add(@Validated@RequestBody User user){
        return AjaxResult.success(userService.insertUser(user));
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        return AjaxResult.success(userService.deleteUserById(id));
    }

    @ApiOperation(value = "根据id查询用户列表", notes = "根据id查询用户列表")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(userService.selectUserById(id));
    }

    @ApiOperation("修改用户信息")
    @PutMapping
    public AjaxResult edit(@RequestBody User user){
        return AjaxResult.success(userService.updateUser(user));
    }
}
